package cn.lele.reggie.controller;

import ch.qos.logback.core.util.TimeUtil;
import cn.lele.reggie.common.R;
import cn.lele.reggie.dto.DishDto;
import cn.lele.reggie.dto.SetmealDto;
import cn.lele.reggie.entity.Dish;
import cn.lele.reggie.entity.Setmeal;
import cn.lele.reggie.entity.SetmealDish;
import cn.lele.reggie.service.DishService;
import cn.lele.reggie.service.SetmealDishService;
import cn.lele.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 分页+条件查询套餐
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> sellectAll(Integer page, Integer pageSize,String name){
        log.info("开始分页，name："+name);
        Page<SetmealDto> objectPage=setmealService.pageSetmealDto(page,pageSize,name);
        return R.success(objectPage);
    }

    /**
     * 添加套餐setmealdto（套餐数据+套餐中菜品的数据）
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> insert(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加套餐成功");
    }

    /**
     * 删除套餐和对应的套餐菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long[] ids){
        setmealService.deleteWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 修改套餐状态
     * @param statusId
     * @param ids
     * @return
     */
    @PostMapping("/status/{statusId}")
    public R<String> updateStatus(@PathVariable Integer statusId,Long[] ids){
        setmealService.updateStatus(statusId,ids);
        return R.success("修改状态成功");
    }

    /***
     * 根据套餐id查dto，修改回显
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<SetmealDto> selectDtoById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.selectDtoById(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐数据
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateDto(setmealDto);
        return R.success("修改套餐成功");
    }

    /**
     * 用户查看所有套餐
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R<List<SetmealDto>> list(Long categoryId,Integer status){
        String key = "Setmeal_"+categoryId.toString();
        List<SetmealDto> list;
        //先从redis中获取缓存数据
        list = (List<SetmealDto>) redisTemplate.opsForValue().get(key);
        if(list != null){
            //如果存在，直接返回，无需查询数据库
            return R.success(list);
        }
        list = setmealService.listSetmeal(categoryId,status);
        redisTemplate.opsForValue().set(key,list,60, TimeUnit.MINUTES);
        return R.success(list);
    }

    /**
     *根据套餐id返回套餐中菜品所有信息
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public R<List<SetmealDish>> setmealShowDish(@PathVariable Long id){
        List<SetmealDish> list=setmealService.listDish(id);
        return R.success(list);
    }

}
